﻿/*===================================================
* CLR版本  : 4.0.30319.42000 
* 类名称   : UIComponent
* 文 件 名 : UIComponent 
* 类描述   :
* 创建人   : oream
* 创建时间 : 2018/2/13 20:17:39
* 修改人   : 
* 修改时间 :
* 版本： @version 1.0
=====================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using System.Threading.Tasks;
using UnityEngine;

namespace Hotfix
{
  [ObjectSystem]
  public class UIComponentSystem : ObjectSystem<UIComponent>, ILoad, IAwake
  {
    public void Awake()
    {
      this.Get().Awake();
    }

    public void Load()
    {
      this.Get().Load();
    }
  }


  public class UIComponent : Component
  {
    private GameObject RootCavas;
    private Dictionary<EnumUiType, AbsUIFactory> dicUiTypes = new Dictionary<EnumUiType, AbsUIFactory>();
    private Dictionary<EnumUiType, UI> dicUis = new Dictionary<EnumUiType, UI>();

    public override void Dispose()
    {
      if (this.id == 0)
      {
        return;
      }
      base.Dispose();
      foreach (var item in dicUis.Values)
      {
        item.Dispose();
      }
      dicUis.Clear();
    }

    public void Awake()
    {
      var obj = Resources.Load<GameObject>("Canvas");
      this.RootCavas = GameObject.Instantiate(obj);
      GameObject.DontDestroyOnLoad(this.RootCavas);
      this.Load();
    }

    public void Load()
    {
      dicUiTypes.Clear();

      Debug.Log("调用成功");

      Type[] types = DllHelper.GetDllTypes();

      foreach (var type in types)
      {

        object[] attribute = type.GetCustomAttributes(typeof(UIFactoryAttribute), false);
        if (attribute.Length == 0)
        {
          continue;
        }

        if (!(attribute[0] is UIFactoryAttribute)) continue;

        UIFactoryAttribute uiFactory = attribute[0] as UIFactoryAttribute;

        Debug.Log(type);

        if (dicUiTypes.ContainsKey((EnumUiType)uiFactory.Type))
        {
          Debug.LogError($"已经存在同类UI Factory: {uiFactory.Type}");
          throw new Exception($"已经存在同类UI Factory: {uiFactory.Type}");
        }
        object obj = Activator.CreateInstance(type);
        AbsUIFactory uIFactory = obj as AbsUIFactory;
        if (uIFactory == null)
        {
          Debug.LogError($"{obj.GetType().FullName} 没有继承 IUIFactory");
          continue;
        }
        this.dicUiTypes.Add((EnumUiType)uiFactory.Type, uIFactory);
      }

    }

    /// <summary>
    /// 创建组件
    /// </summary>
    /// <param name="UiType"></param>
    /// <returns></returns>
    public UI Create(EnumUiType UiType)
    {
      try
      {
        Debug.Log(UiType);
        UI ui = this.dicUiTypes[UiType].Create(this.GetParent<Scene>(), UiType, RootCavas);
        this.dicUis.Add(UiType, ui);
        UIManager.Instance.OpenUI(UiType.ToString());
        return ui;
      }
      catch (Exception e)
      {
        throw new Exception($"{UiType} UI 错误: {e.ToString()}"); ;
      }
    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="uiType"></param>
    /// <param name="ui"></param>
    public void Add(EnumUiType uiType, UI ui)
    {
      this.dicUis.Add(uiType, ui);
    }

    /// <summary>
    /// 移除
    /// </summary>
    /// <param name="type"></param>
    public void Remove(EnumUiType type)
    {
      UI ui;
      if (!dicUis.TryGetValue(type, out ui))
      {
        return;
      }
      dicUiTypes[type].Remove(type);
      dicUis.Remove(type);
      ui.Dispose();
      UIManager.Instance.CloseUI(type.ToString());
    }

    public void RemoveAll()
    {
      foreach (EnumUiType type in this.dicUis.Keys.ToArray())
      {
        UI ui;
        if (!this.dicUis.TryGetValue(type, out ui))
        {
          continue;
        }
        this.dicUis.Remove(type);
        ui.Dispose();
      }
    }

    public UI Get(EnumUiType type)
    {
      UI ui;
      this.dicUis.TryGetValue(type, out ui);
      return ui;
    }

    public List<EnumUiType> GetUITypeList()
    {
      return new List<EnumUiType>(this.dicUis.Keys);
    }

  
  }
}
